package org.example.erat.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.dto.CourseDTO;
import org.example.erat.dto.ResponseDTO;
import org.example.erat.service.CourseService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/courses")
@RequiredArgsConstructor
@Slf4j
public class CourseController {

    private final CourseService courseService;

    /**
     * 创建课程
     * @param courseDTO 课程DTO
     * @return 响应
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseDTO<CourseDTO> createCourse(@Valid @RequestBody CourseDTO courseDTO) {
        log.info("创建课程请求: {}", courseDTO);
        CourseDTO createdCourse = courseService.createCourse(courseDTO);
        return ResponseDTO.success("课程创建成功", createdCourse);
    }

    /**
     * 更新课程
     * @param id 课程ID
     * @param courseDTO 课程DTO
     * @return 响应
     */
    @PutMapping("/{id}")
    public ResponseDTO<CourseDTO> updateCourse(@PathVariable Long id, @Valid @RequestBody CourseDTO courseDTO) {
        log.info("更新课程请求: id={}, course={}", id, courseDTO);
        CourseDTO updatedCourse = courseService.updateCourse(id, courseDTO);
        return ResponseDTO.success("课程更新成功", updatedCourse);
    }

    /**
     * 删除课程
     * @param id 课程ID
     * @return 响应
     */
    @DeleteMapping("/{id}")
    public ResponseDTO<Void> deleteCourse(@PathVariable Long id) {
        log.info("删除课程请求: id={}", id);
        courseService.deleteCourse(id);
        return ResponseDTO.success("课程删除成功", null);
    }

    /**
     * 获取课程
     * @param id 课程ID
     * @return 响应
     */
    @GetMapping("/{id}")
    public ResponseDTO<CourseDTO> getCourse(@PathVariable Long id) {
        log.info("获取课程请求: id={}", id);
        CourseDTO course = courseService.getCourse(id);
        return ResponseDTO.success(course);
    }

    /**
     * 获取所有课程
     * @return 响应
     */
    @GetMapping
    public ResponseDTO<List<CourseDTO>> getAllCourses() {
        log.info("获取所有课程请求");
        List<CourseDTO> courses = courseService.getAllCourses();
        return ResponseDTO.success(courses);
    }
}
